import Vue from 'vue'
import Router from 'vue-router'

import constRoutersMap from './constRouters'

// 处理登陆时获取路由跳转俩次抛出异常
const routerPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(error => error)
}

Vue.use(Router)

const createRouter = () => new Router({
  base: process.env.BASE_URL,
  scrollBehavior: () => ({ y: 0 }),
  routes: [
    ...constRoutersMap
  ]
})

const router = createRouter()

// 清空路由
export const removeRouer = () => {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher
}

// 默认路由
export default router
